Welcome Guest | Sign in | Register

Home > Java Programming > Inner Classes > Questions and Answers

01.  What will be the output of the program?
public class HorseTest
{
public static void main (String [] args)
{
class Horse
{
public String name; /* Line 7 */
public Horse(String s)
{
name = s;
}
} /* class Horse ends */

Object obj = new Horse("Zippo"); /* Line 13 */
Horse h = (Horse) obj; /* Line 14 */
System.out.println(h.name);
}
} /* class HorseTest ends */
A. An exception occurs at runtime at line 10. B. It prints "Zippo".
C. Compilation fails because of an error on line 7. D. Compilation fails because of an error on line 13.

Answer and Explanation

Answer: It prints "Zippo".

Explanation:
The code in the HorseTest class is perfectly legal. Line 13 creates an instance of the method-local inner class Horse, using a reference variable declared as type Object. Line 14 casts the Horse object to a Horse reference variable, which allows line 15 to compile. If line 14 were removed, the HorseTest code would not compile, because class Object does not have a name variable.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02. What will be the output of the program?

public abstract class AbstractTest
{
public int getNum()
{
return 45;
}
public abstract class Bar
{
public int getNum()
{
return 38;
}
}
public static void main (String [] args)
{
AbstractTest t = new AbstractTest()
{
public int getNum()
{
return 22;
}
};
AbstractTest.Bar f = t.new Bar()
{
public int getNum()
{
return 57;
}
};

System.out.println(f.getNum() + " " + t.getNum());
}
}
A. 57 22 B. 45 38
C. 45 57 D. An exception occurs at runtime.

Answer and Explanation

Answer: 57 22

Explanation:
You can define an inner class as abstract, which means you can instantiate only concrete subclasses of the abstract inner class. The object referenced by the variable t is an instance of an anonymous subclass of AbstractTest, and the anonymous class overrides the getNum() method to return 22. The variable referenced by f is an instance of an anonymous subclass of Bar, and the anonymous Bar subclass also overrides the getNum() method (to return 57). Remember that to instantiate a Bar instance, we need an instance of the enclosingAbstractTest class to tie to the new Bar inner class instance. AbstractTestcan't be instantiated because it's abstract, so we created an anonymous subclass (non-abstract) and then used the instance of that anonymous subclass to tie to the new Bar subclass instance.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. Which is true about an anonymous inner class?
A. It can extend exactly one class and implement exactly one interface. B. It can extend exactly one class and can implement multiple interfaces.
C. It can extend exactly one class or implement exactly one interface. D. It can implement multiple interfaces regardless of whether it also extends a class.

Answer and Explanation

Answer: It can extend exactly one class or implement exactly one interface.

Explanation:
Option C is correct because the syntax of an anonymous inner class allows for only one named type after the new, and that type must be either a single interface (in which case the anonymous class implements that one interface) or a single class (in which case the anonymous class extends that one class).
Option A, B, D, and E are all incorrect because they don't follow the syntax rules described in the response for answer Option C.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04.  class Foo
{
class Bar{ }
}
class Test
{
public static void main (String [] args)
{
Foo f = new Foo();
/* Line 10: Missing statement ? */
}
}
which statement, inserted at line 10, creates an instance of Bar? 
A. Foo.Bar b = new Foo.Bar(); B. Foo.Bar b = f.new Bar();
C. Bar b = new f.Bar(); D. Bar b = f.new Bar();

Answer and Explanation

Answer: Foo.Bar b = f.new Bar();

Explanation:
Option B is correct because the syntax is correct-using both names (the enclosing class and the inner class) in the reference declaration, then using a reference to the enclosing class to invoke new on the inner class.
Option A, C and D all use incorrect syntax. A is incorrect because it doesn't use a reference to the enclosing class, and also because it includes both names in the new.
C is incorrect because it doesn't use the enclosing class name in the reference variable declaration, and because the new syntax is wrong.
D is incorrect because it doesn't use the enclosing class name in the reference variable declaration.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
05. Given below the sample code :
class SuperClass {
SuperClass() {
}

static class SubClass {
SubClass() { }
}
}
Which is the correct way to declare the object of "Subclass" , which can be use outside the "Superclass" ?
A. SubClass Sub = new SuperClass().new SubClass(); B. SubClass Sub = new SuperClass.SubClass();
C. SuperClass.SubClass Sub = new SuperClass.SubClass(); D. SubClass SubClass Sub = new SuperClass.SubClass();

Answer and Explanation

Answer: SuperClass.SubClass Sub = new SuperClass.SubClass();

Explanation:
Because the inner class "Subclass" is declared 'static' that's why it is declared like this.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
06. Given below the sample code :
class SuperClass {
SuperClass() {
}

public class SubClass {
SubClass() { }
}
}
Which is the correct way to declare the object of public "Subclass" , which can be use outside the "Superclass" ?
A. SubClass Sub = new SuperClass().new SubClass(); B. SubClass Sub = new SuperClass.SubClass();
C. SuperClass.SubClass Sub = new SuperClass.SubClass(); D. SuperClass.SubClass nbc = new SuperClass().new SubClass();

Answer and Explanation

Answer: SuperClass.SubClass nbc = new SuperClass().new SubClass();

Explanation:
If the "Superclass" is declared as "static" it will be declared like this :
SuperClass.SubClass Sub = new SuperClass.SubClass();
But if the "Superclass" is declared as "public" it will be declared like this :
SuperClass.SubClass nbc = new SuperClass().new SubClass();

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
07. Given below the sample code :
class Demo{
demo() {
}
public class Demo {
funtion() { }
}
}
How can we correct the above code? Select appropriate option :
A. inner class should be declare as "static" B. inner class must also declared it's constructor.
C. change the name of the inner class. D. no need of correction

Answer and Explanation

Answer: change the name of the inner class.

Explanation:
An inner class can't have the same name as its enclosing class.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
08. Given below the sample code :
class SuperClass {
public static void main(String[] args) {
new SubClass();
}

SuperClass() {
System.out.print("Inside SuperClass ");
}
}

class SubClass extends SuperClass {
SubClass() {
System.out.print("Inside SubClass");
}
}
What is the output of the following code ?
A. Inside SuperClass  B. Inside SubClass
C.  It will give compile error D. Inside SuperClass Inside SubClass.

Answer and Explanation

Answer: Inside SuperClass Inside SubClass.

Explanation:
Because the 'SuperClass' constructor automatically run, therefore the first output would be "Inside SuperClass" & after it, the instance of the "SubClass" is called which in turn prints "Inside SubClass".

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
09. What is the output for the below code ?

public class Outer {
private int a = 7;

class Inner {
public void displayValue() {
System.out.println("Value of a is " + a);
}
}
}


public class Test {

public static void main(String... args) throws Exception {
Outer mo = new Outer();
Outer.Inner inner = mo.new Inner();
inner.displayValue();

}

}


A. Value of a is 7 B. Compile Error - not able to access private member.
C. Runtime Exception D. Value of a is 8

Answer and Explanation

Answer: Value of a is 7

Explanation:
An inner class instance can never stand alone without a direct relationship to an instance of the outer class.
you can access the inner class is through a live instance of the outer class.
Inner class can access private member of the outer class.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.